package com.leji.song.mainPage;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.dgl.sdk.util.JsonUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.extra.LoginActivity;
import com.leji.song.extra.MyApplication;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.mainPage.model.*;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by colin.ding on 2014/6/4.
 */
public class BuyFruitActivity extends ShopMenuActivity{

    protected void initView() {
        setActivityTitle("水果外卖");
        setReturnBtn();

        foodListView = (ListView) findViewById(R.id.foodListView);
        goodsTypeListView = (ListView) findViewById(R.id.goodsTypeListView);

        translatePriceInfo = (TextView) findViewById(R.id.translatePriceInfo);
        money = (TextView) findViewById(R.id.money);
        submit = (Button) findViewById(R.id.submit);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(buyGoodsResultList.size()==0){
                    showToast("请选择水果！");
                }else{

                    if(MyApplication.isLogin){
                        Intent intent = new Intent(BuyFruitActivity.this, SubmitOrderActivity.class);
                        intent.putExtra("fromWho", "fruit");
                        intent.putExtra("orderList", (Serializable) buyGoodsResultList);
                        intent.putExtra("translatePrice", (Serializable) tPriceList);
                        startActivity(intent);
                    }else{
                        showActivity(LoginActivity.class);
                    }

                }
            }
        });
    }



    protected void getGoodsType() {

        BaseAjaxParams params = new BaseAjaxParams();

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetFruitTypes.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity) this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                Object obj = NomalResult.doResult(o, BuyFruitActivity.this);
                if (obj != null) {

                    Gson gson = new Gson();


                    try {
                        Type listType = new TypeToken<List<DS_GoodsType>>() {
                        }.getType();

                        List<DS_GoodsType> allTypeList = new ArrayList<DS_GoodsType>();
                        DS_GoodsType allType = new DS_GoodsType();
                        allType.setName("全部");
                        allType.setId(null);  //id 为空， 拿所有的。

                        allTypeList.add(allType);

                        List<DS_GoodsType> typesList = gson.fromJson(gson.toJson(obj), listType);

                        allTypeList.addAll(typesList);

                        GoodsTypeAdapter adapter = new GoodsTypeAdapter(BuyFruitActivity.this,
                                allTypeList, new GoodsTypeAdapter.GoodsTypeClickCallback() {
                            @Override
                            public void onClick(String typeID) {
                                getMenuByID(typeID);
                            }
                        }
                        );

                        goodsTypeListView.setAdapter(adapter);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    protected void getMenu(String type) {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetFruitList.params.size, "20");
        params.put(HttpInterface.HIF_GetFruitList.params.no, "1");
        if(type!=null){
            params.put(HttpInterface.HIF_GetFruitList.params.type, type);
        }

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetFruitList.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o,BuyFruitActivity.this);
                if (obj != null) {

                    Gson gson = new Gson();

                    DS_NormalResult result = JsonUtil.fromJson(gson.toJson(obj), DS_NormalResult.class);

                    try {
                        Object dataObj = result.getDatas();
                        if(dataObj!= null){
                            Type listType = new TypeToken<List<DS_Goods>>() {}.getType();
                            List<DS_Goods> goodsList = gson.fromJson(gson.toJson(dataObj), listType);

                            GoodsAdapter goodsAdapter = new GoodsAdapter(BuyFruitActivity.this, goodsList, new GoodsAdapter.BuyGoodsCallback() {
                                @Override
                                public void onClick(DS_Goods goods) {
                                    calcBuyGoodsResult(goods);
                                }
                            });
                            foodListView.setAdapter(goodsAdapter);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        });
    }


    @Override
    protected void getTranslatePriceInfo() {
        BaseAjaxParams params = new BaseAjaxParams();

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetFruitTransPrice.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity) this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o, BuyFruitActivity.this);
                if (obj != null) {

                    try {

                        Gson gson = new Gson();
                        Type listType = new TypeToken<List<DS_TranslatePrice>>() {
                        }.getType();

                        tPriceList = gson.fromJson(gson.toJson(obj), listType);

                        if (tPriceList.size() > 0) {
                            translatePriceInfo.setText(tPriceList.get(0).getName());
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }
            }
        });
    }
}
